
package com.hlkj.pay.controller.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.order.impl.OrderNotifyHandler;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.order.NotifyOrderQueryDto;
import com.hlkj.pay.infrastructure.model.order.OrderNotifyDO;
import com.hlkj.pay.service.order.IOrderNotifyService;
import com.hlkj.pay.vo.order.req.NotifyOrderPageQueryReq;
import com.hlkj.pay.vo.order.resp.OrderNotifyResp;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;

/**
 * @author HlpayTeam
 * @date 2024/10/23 11:36
 */
@Tag(name = "订单通知管理")
@RestController
@RequestMapping("/v1/order/notify")
@RequiredArgsConstructor
@Slf4j
public class NotifyOrderController extends BaseController {

    private final IOrderNotifyService orderNotifyService;

    private final OrderNotifyHandler orderNotifyHandler;

    @Operation(summary = "通知列表")
    @GetMapping("/page")
    CommonResult<PageResult<OrderNotifyResp>> queryPayOrderPage(NotifyOrderPageQueryReq notifyOrderPageQueryReq) {
        NotifyOrderQueryDto notifyOrderQueryDto = BeanUtil.copy(notifyOrderPageQueryReq, NotifyOrderQueryDto.class);
        processQueryDate(notifyOrderPageQueryReq, notifyOrderQueryDto);
        // processQuerySn(notifyOrderPageQueryReq, notifyOrderQueryDto);
        PageResult<OrderNotifyDO> page = orderNotifyService.queryPage(notifyOrderQueryDto);
        List<OrderNotifyDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        List<OrderNotifyResp> payOrderRespList = cn.hutool.core.bean.BeanUtil.copyToList(list, OrderNotifyResp.class);
        processMerchantList(payOrderRespList);
        return buildPageResult(page, payOrderRespList);
    }

    @Operation(summary = "通知详情")
    @GetMapping("/detail")
    CommonResult<OrderNotifyResp> queryPayOrderDetail(@RequestParam("id") Long id) {
        OrderNotifyDO orderNotifyDO = orderNotifyService.queryDetail(id);
        if (orderNotifyDO == null) {
            return CommonResult.success(null);
        }
        OrderNotifyResp adPayOrderResp = BeanUtil.copyProperties(orderNotifyDO, OrderNotifyResp.class);
        return CommonResult.success(adPayOrderResp);
    }

    @Operation(summary = "重发通知")
    @GetMapping("/retry")
    CommonResult<Void> retryNotify(@RequestParam("id") Long id) {
        OrderNotifyDO orderNotifyDO = orderNotifyService.queryDetail(id);
        if (orderNotifyDO == null) {
            return CommonResult.success(null);
        }
        orderNotifyHandler.retryNotify(orderNotifyDO);
        return CommonResult.success();
    }

}
